package com.neusoft.nepb.controller;

import com.neusoft.nepb.common.AlertUtils;
import com.neusoft.nepb.common.SceneUtils;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.scene.control.RadioButton;
import javafx.scene.control.TextField;
import javafx.scene.control.ToggleGroup;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.stage.Stage;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SeclectNetViewController {
    @FXML
    ImageView imageViewMap,imageViewBackground;
    @FXML
    ComboBox<String> cmbProvince,cmbCity,cmbDistrict;
    @FXML
    private TextField txt_detailAddress;


    private Map<String, List<String>> provinceMap = new HashMap<>();
    private Map<String,List<String>> cityMap = new HashMap<>();
    public String province,city,district,detailAddress;
    @FXML
    public void initialize(){
        //初始化背景图片
       SceneUtils.setImage(imageViewBackground,"/com/neusoft/nepb/image/sun.jpg");
        //初始化地图图片(设置具体参数所以无法使用封装方法
        Image imageMap = new Image("/com/neusoft/nepb/image/map.gif");
        imageViewMap.setImage(imageMap);
        imageViewMap.setFitWidth(457);
        imageViewMap.setFitHeight(301);
        imageViewMap.setPreserveRatio(false);
        //初始化下拉列表省市数据
        provinceMap.put("辽宁省", List.of("沈阳市", "大连市"));
        provinceMap.put("北京市", List.of("北京市"));
        provinceMap.put("天津市", List.of("天津市"));
        provinceMap.put("河北省", List.of("石家庄市", "唐山市"));
        provinceMap.put("山西省", List.of("太原市", "大同市"));
        provinceMap.put("内蒙古自治区", List.of("呼和浩特市", "包头市"));
        provinceMap.put("吉林省", List.of("长春市", "吉林市"));
        provinceMap.put("黑龙江省", List.of("哈尔滨市", "大庆市"));
        provinceMap.put("上海市", List.of("上海市"));
        provinceMap.put("江苏省", List.of("南京市", "苏州市"));
        provinceMap.put("浙江省", List.of("杭州市", "宁波市"));
        provinceMap.put("安徽省", List.of("合肥市", "黄山市"));
        provinceMap.put("福建省", List.of("福州市", "厦门市"));
        provinceMap.put("江西省", List.of("南昌市", "九江市"));
        provinceMap.put("山东省", List.of("济南市", "青岛市"));
        provinceMap.put("河南省", List.of("郑州市", "洛阳市"));
        provinceMap.put("湖北省", List.of("武汉市", "宜昌市"));
        provinceMap.put("湖南省", List.of("长沙市", "张家界市"));
        provinceMap.put("广东省", List.of("广州市", "深圳市"));
        provinceMap.put("广西壮族自治区", List.of("南宁市", "桂林市"));
        provinceMap.put("海南省", List.of("海口市", "三亚市"));
        provinceMap.put("重庆市", List.of("重庆市"));
        provinceMap.put("四川省", List.of("成都市", "绵阳市"));
        provinceMap.put("贵州省", List.of("贵阳市", "遵义市"));
        provinceMap.put("云南省", List.of("昆明市", "大理市"));
        provinceMap.put("西藏自治区", List.of("拉萨市", "林芝市"));
        provinceMap.put("陕西省", List.of("西安市", "延安市"));
        provinceMap.put("甘肃省", List.of("兰州市", "敦煌市"));
        provinceMap.put("青海省", List.of("西宁市", "格尔木市"));
        provinceMap.put("宁夏回族自治区", List.of("银川市", "中卫市"));
        provinceMap.put("新疆维吾尔自治区", List.of("乌鲁木齐市", "喀什市"));
        provinceMap.put("香港特别行政区", List.of("香港"));
        provinceMap.put("澳门特别行政区", List.of("澳门"));

        // 初始化下拉列表市区数据
        cityMap.put("北京市", List.of("朝阳区", "海淀区", "东城区"));
        cityMap.put("天津市", List.of("和平区", "河西区", "南开区"));
        cityMap.put("石家庄市", List.of("长安区", "桥西区", "裕华区"));
        cityMap.put("唐山市", List.of("路南区", "路北区", "丰南区"));
        cityMap.put("太原市", List.of("迎泽区", "杏花岭区", "万柏林区"));
        cityMap.put("大同市", List.of("平城区", "云冈区", "新荣区"));
        cityMap.put("呼和浩特市", List.of("新城区", "回民区", "赛罕区"));
        cityMap.put("包头市", List.of("昆都仑区", "青山区", "东河区"));
        cityMap.put("沈阳市", List.of("和平区", "浑南区", "沈河区"));
        cityMap.put("大连市", List.of("中山区", "西岗区", "沙河口区"));
        cityMap.put("长春市", List.of("朝阳区", "南关区", "宽城区"));
        cityMap.put("吉林市", List.of("船营区", "昌邑区", "龙潭区"));
        cityMap.put("哈尔滨市", List.of("南岗区", "道里区", "香坊区"));
        cityMap.put("大庆市", List.of("萨尔图区", "龙凤区", "让胡路区"));
        cityMap.put("上海市", List.of("黄浦区", "徐汇区", "浦东新区"));
        cityMap.put("南京市", List.of("玄武区", "秦淮区", "建邺区"));
        cityMap.put("苏州市", List.of("姑苏区", "吴中区", "工业园区"));
        cityMap.put("杭州市", List.of("上城区", "西湖区", "拱墅区"));
        cityMap.put("宁波市", List.of("海曙区", "江北区", "鄞州区"));
        cityMap.put("合肥市", List.of("蜀山区", "包河区", "庐阳区"));
        cityMap.put("黄山市", List.of("屯溪区", "黄山区", "徽州区"));
        cityMap.put("福州市", List.of("鼓楼区", "台江区", "仓山区"));
        cityMap.put("厦门市", List.of("思明区", "湖里区", "集美区"));
        cityMap.put("南昌市", List.of("东湖区", "西湖区", "青云谱区"));
        cityMap.put("九江市", List.of("濂溪区", "浔阳区", "柴桑区"));
        cityMap.put("济南市", List.of("历下区", "市中区", "槐荫区"));
        cityMap.put("青岛市", List.of("市南区", "市北区", "崂山区"));
        cityMap.put("郑州市", List.of("金水区", "二七区", "中原区"));
        cityMap.put("洛阳市", List.of("涧西区", "西工区", "洛龙区"));
        cityMap.put("武汉市", List.of("武昌区", "江汉区", "硚口区"));
        cityMap.put("宜昌市", List.of("西陵区", "伍家岗区", "点军区"));
        cityMap.put("长沙市", List.of("芙蓉区", "天心区", "岳麓区"));
        cityMap.put("张家界市", List.of("永定区", "武陵源区", "慈利县"));
        cityMap.put("广州市", List.of("越秀区", "天河区", "海珠区"));
        cityMap.put("深圳市", List.of("福田区", "南山区", "罗湖区"));
        cityMap.put("南宁市", List.of("青秀区", "兴宁区", "西乡塘区"));
        cityMap.put("桂林市", List.of("象山区", "秀峰区", "七星区"));
        cityMap.put("海口市", List.of("秀英区", "龙华区", "琼山区"));
        cityMap.put("三亚市", List.of("崖州区", "天涯区", "吉阳区"));
        cityMap.put("重庆市", List.of("渝中区", "江北区", "南岸区"));
        cityMap.put("成都市", List.of("锦江区", "青羊区", "武侯区"));
        cityMap.put("绵阳市", List.of("涪城区", "游仙区", "安州区"));
        cityMap.put("贵阳市", List.of("云岩区", "南明区", "观山湖区"));
        cityMap.put("遵义市", List.of("红花岗区", "汇川区", "播州区"));
        cityMap.put("昆明市", List.of("五华区", "盘龙区", "西山区"));
        cityMap.put("大理市", List.of("大理市下关镇", "大理市大理镇", "大理市喜洲镇"));
        cityMap.put("拉萨市", List.of("城关区", "堆龙德庆区", "达孜区"));
        cityMap.put("林芝市", List.of("巴宜区", "米林县", "工布江达县"));
        cityMap.put("西安市", List.of("碑林区", "雁塔区", "未央区"));
        cityMap.put("延安市", List.of("宝塔区", "安塞区", "子长市"));
        cityMap.put("兰州市", List.of("城关区", "七里河区", "西固区"));
        cityMap.put("敦煌市", List.of("沙洲镇", "肃州镇", "莫高镇"));
        cityMap.put("西宁市", List.of("城东区", "城中区", "城西区"));
        cityMap.put("格尔木市", List.of("昆仑路街道", "黄河路街道", "河西街道"));
        cityMap.put("银川市", List.of("兴庆区", "金凤区", "西夏区"));
        cityMap.put("中卫市", List.of("沙坡头区", "中宁县", "海原县"));
        cityMap.put("乌鲁木齐市", List.of("天山区", "沙依巴克区", "新市区"));
        cityMap.put("喀什市", List.of("恰萨街道", "吾斯塘博依街道", "东湖街道"));
        cityMap.put("香港", List.of("中西区", "湾仔区", "东区"));
        cityMap.put("澳门", List.of("澳门半岛", "氹仔", "路环"));
        cmbProvince.setItems(FXCollections.observableArrayList(provinceMap.keySet()));
        cmbProvince.getSelectionModel().clearSelection();
        cmbProvince.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<String>() {
            @Override
            public void changed(ObservableValue<? extends String> observableValue, String oldValue, String newValue) {
                List<String> citys = provinceMap.getOrDefault(newValue,List.of());
                cmbCity.setItems(FXCollections.observableArrayList(citys));
                cmbCity.getSelectionModel().clearSelection();
                cmbDistrict.getItems().clear();
            }
        });
        cmbCity.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<String>() {
            @Override
            public void changed(ObservableValue<? extends String> observableValue, String oldValue, String newValue) {
                List<String> districts = cityMap.getOrDefault(newValue,List.of());
                cmbDistrict.setItems(FXCollections.observableArrayList(districts));
                cmbDistrict.getSelectionModel().clearSelection();
            }
        });
    }



    public void handleUploadData(ActionEvent event) throws IOException {
        SceneUtils.switchTo(event,"/com/neusoft/nepb/view/UploadDataView.fxml");

        //将选择的信息写入一个临时文件，便于在下个程序内读取
        province = cmbProvince.getValue();
        city = cmbCity.getValue();
        district = cmbDistrict.getValue();
        detailAddress = txt_detailAddress.getText();
        Path filePath = Paths.get("location.txt");
        try {
            // 确保文件存在（如果不存在则创建）
            if (Files.notExists(filePath)) {
                Files.createFile(filePath);
            }

            //清除文件中原有数据
            try(FileWriter fileWriter = new FileWriter("location.txt")){

            }catch (IOException e){
                System.err.println("清除出错");
            }

            try (BufferedWriter writer = Files.newBufferedWriter(Paths.get("location.txt"))) {
                writer.write(province + "\n");
                writer.write(city + "\n");
                writer.write(district + "\n");
                writer.write(detailAddress);
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (IOException e) {
            System.err.println("写入文件失败: " + e.getMessage());
        }
    }
    public void handleInfoSave(ActionEvent event) throws IOException {
        SceneUtils.switchTo(event,"/com/neusoft/nepb/view/InfoSaveView.fxml");
    }

}
